﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Web;

namespace Kazuku.Core.Helpers {
	public class SerializationHelper {
		public static byte[] SerializeToBinary(object source) {
			byte[] buffer;

			using (MemoryStream ms = new MemoryStream()) {
				BinaryFormatter bFormatter = new BinaryFormatter();
				// Serialize the class into the MemoryStream 
				bFormatter.Serialize(ms, source);
				//ms.Seek(0,0); 
				// Write the stream contents to a byte array
				buffer = ms.ToArray();
			}

			return buffer;
		}
		public static object DeserializeFromBinary(byte[] buffer) {
			Object deserializedObject = null;

			if (buffer != null) {
				using (MemoryStream ms = new MemoryStream()) {
					// Write the byte array to a stream
					ms.Write(buffer, 0, buffer.Length);
					ms.Seek(0, 0);
					BinaryFormatter b = new BinaryFormatter();
					// Deserialize the stream to an object
					deserializedObject = b.Deserialize(ms);
				}
			}

			return deserializedObject;
		}
	}
}